<?php
class Address extends CS_Controller
{
    public function _init()
    {
        $this->load->model('tourism_delivery_address_model', 'tourism_delivery_address');
        $this->load->model('region_model', 'region');
    }

     /**
     * 添加 和 编辑--address
     */
    public function insertAddress()
    {
        $param = $this->input->post();
        $param['uid'] = $this->uid;
        $error = $this->validate($param);
        if (!empty($error)) {
        	$this->jsonMessage('请把收货地址填完','');
        }
        if (isset($param['address_id'])) {
        	$this->editPost($param);
        }else{
        	$this->addPost($param);
        }
    }
    
     /**
     * 添加产品
     * @param unknown $param
     */
    public function addPost($param)
    {
        if ($param['is_default'] == 1) {
            $result = $this->tourism_delivery_address->insert($param);
        } else {
            $this->db->trans_start();
            $this->tourism_delivery_address->setNonDefault($this->uid);
            $result = $this->tourism_delivery_address->insert($param);
            $this->db->trans_complete();
        }
        if ($result) {
            $data['allAddress'] = $this->tourism_delivery_address->findOne(['uid' => $param['uid']]);
            $this->jsonMessage('', $this->load->view('tourism/address/list_address', $data, true));
        }
        $this->jsonMessage('请刷新网络重试');
    }


    /**
     * 编辑
     */
    public function editAddress()
    {
        $address_id = $this->input->post('address_id');
        $address = $this->tourism_delivery_address->findOne(['address_id' => $address_id]);
        if ($address->num_rows() <= 0) {
            $this->jsonMessage('此收货地址没有');
        }
        $data['address'] = $address->row();
        $this->jsonMessage('', $this->load->view('tourism/address/edit_infor', $data, true));
    }

     /**
     * 编辑添加
     * @param unknown $param
     */
    public function editPost($param)
    {
        if ($param['is_default'] == 1) {
            $result = $this->tourism_delivery_address->updateAddress($param);
        } else {
            $this->db->trans_start();
            $resultX = $this->tourism_delivery_address->setNonDefault($this->uid);
            $result = $this->tourism_delivery_address->updateAddress($param);
            $this->db->trans_complete();
        }
        if ($result) {
            $data['allAddress'] = $this->tourism_delivery_address->findOne(['uid' => $this->uid]);
            $this->jsonMessage('',$this->load->view('tourism/address/list_address',$data,true));
        }
        $this->jsonMessage('编辑失败','');
    }

     /**
     * 删除地址
     * @param unknown $id
     */
    public function delete()
    {
        $address_id = $this->input->post('address_id');
        $result = $this->tourism_delivery_address->delete(['address_id' => $address_id, 'uid' => $this->uid]);
        if ($result) {
            $data['allAddress'] = $this->tourism_delivery_address->findOne(['uid' => $this->uid]);
            $this->jsonMessage('', $this->load->view('tourism/address/list_address', $data, true));
        }
        $this->jsonMessage('删除失败');
    }

    public function setDefaultAddress()
    {
        $id = $this->input->post('id');
        $non = $this->tourism_delivery_address->setNonDefault($this->uid);
        $result = $this->tourism_delivery_address->setDefault($id);
        if($result){
            $this->jsonMessage('','地址设置成功');
        }else{
            $this->jsonMessage('地址有误，地址设置失败');
        }
    }

    public function ajaxRegion()
    {
        $type = $this->input->post('type');
        $id = $this->input->post('id');
        $data['result'] = $this->region->findByType($id,$type);
        echo json_encode(
            array(
                'status' => true,
                'html'=>$this->load->view('tourism/address/region', $data,  true)
            )
        );exit;
    }
    
     /**
     * 验证
     * @param unknown $param
     */
    public function validate($param)
    {
        $error  = array();
        if ($this->validateParam($param['receiver_name'])) {
            $error[] = '收件人姓名不能为空';
        }
        if ($this->validateParam($param['tel'])) {
            $error[] = '联系电话不能为空';
        }
        if (!preg_match("/^1[34578]{1}\d{9}$/",$param['tel'])) {
            $error[] = '联系电话格式出错';
        }
        if ($this->validateParam($param['code'])) {
            $error[] = '邮政编码不能为空';
        }
        if (!preg_match('/^[0-9]{6}$/',$param['code'])) {
        	$error[] = '邮政编码格式出错';
        }
        if ($this->validateParam($param['province_id'])) {
            $error[] = '省不能为空';
        }
        if ($this->validateParam($param['city_id'])) {
            $error[] = '市不能为空';
        }
        if ($this->validateParam($param['district_id'])) {
            $error[] = '县不能为空';
        }
        if ($this->validateParam($param['detailed'])) {
            $error[] = '详细地址不能为空';
        }
    }
}